package cz.possoft.client.utils;

import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

import cz.possoft.client.gui.gamepanel.components.GamePane;
import cz.possoft.client.gui.panel.GamePanel;
import cz.possoft.client.loader.JarDeploymentException;
import cz.possoft.client.loader.LoaderHolder;
import cz.possoft.serializable.generic.Game;
import cz.possoft.serializable.generic.HraciPole;

/**
 * Trida plni staticky ucel na dotaz pro instance hernich panelu a sachovnic atd.
 * 
 * @author mara
 */
public class GameInvocationUtils {
	
	// pole se vsema druhama her. Tyto hry (values) se pak vyplnuji do komboboxu v NastaveniShowPanelu.
	// Keys jsou pouzity k logice (berou se z nich nazvy trid atd)
	//public static TreeMap<String, String> games;
	
	// inicializace dostupnych her.
	/*static {
		try {
			games = new TreeMap<String, String>();
			loaderHolder = new LoaderHolder();
			Set<String> gameKeys = loaderHolder.getKeys();
			for (String gameKey : gameKeys) {
				games.put(gameKey, gameKey + "_val");
			}
		}
		catch (JarDeploymentException jde) {
			jde.printStackTrace();
			throw new RuntimeException(jde);
		}
	    //games.put("Chess", Text.SACHY);	
	    //games.put("Dama", Text.DAMAHRA);	
	    //games.put("Four", Text.PADACI_PISKVORKY);
	    //games.put("Piskvorky", Text.PISKVORKY);
	}	*/
	
	/*public static Map<String, String> getGames() {
		return games;
	}*/
	
	// vraci novou instanci herniho panelu podle klice dane hry.
	public static GamePanel getInstanceOfGamePanel(String gameKey) {		
		GamePanel gamePanel = null;
		try {
			Class<?> c = LoaderHolder.getInstance().getClassForKey(gameKey, "cz.possoft.client." + gameKey.toLowerCase() + ".gui.panel." + gameKey + "Panel");
		    gamePanel = (GamePanel)c.newInstance(); 
		}
		catch (Exception e) {
			System.err.println("Exception pri Java reflection - zakladani GamePanelu.");
			e.printStackTrace();
		}
		return gamePanel;
	}
	
	// vraci novou instanci herniho pole podle klice hry. 
	// V Konstruktoru predano i hraci pole
	public static GamePane getInstanceOfGamePane(String gameKey, HraciPole hp) {						
		GamePane gamePane = null;
		try {
			Class<?> c = LoaderHolder.getInstance().getClassForKey(gameKey, "cz.possoft.client." + gameKey.toLowerCase() + ".gui.components." + gameKey + "Pane");
		    gamePane = (GamePane)c.newInstance(); 
		}
		catch (Exception e) {
			System.err.println("Exception pri Java reflection - zakladani GamePane.");
			e.printStackTrace();
		}
		gamePane.init(hp);
	    return gamePane;	
	}		
	
	// vraci novou instanci hry podle klice hry. 	
	public static Game getInstanceOfGame(String gameKey) {						
		Game game = null;
		try {
			Class<?> c = LoaderHolder.getInstance().getClassForKey(gameKey, "cz.possoft.serializable." + gameKey.toLowerCase() + ".game." + gameKey + "Game");
		    game = (Game)c.newInstance(); 
		}
		catch (Exception e) {
			System.err.println("Exception pri Java reflection - zakladani Game.");
			e.printStackTrace();
		}		
	    return game;	
	}	
	
	   // vraci klic hry podle toho, jakeho je hra typu. Pouzito pri load hry.
    public static String getKeyOfGame(Game game) {
    	for (GamePanel panel : PanelHolderUtils.getAllGamePanels()) {
    		if (panel.getGame().getClass() == game.getClass()) {
    			return panel.getGameClassPrefix();
    		}
    	}
    	// toto by nemelo nastat
    	return null;
    }	

}
